import { defineStore } from 'pinia'
import { ElMessage, ElMessageBox } from 'element-plus'

const Store=defineStore('publicFunction',
   {
    state: () => {
        return {}
    },
    getters:{},
    actions:{
        //删除询问框
        deleteConfirm(e: any, callback: any) {
            let id = e.currentTarget.dataset.id;
            ElMessageBox.confirm("此操作将会永久删除这条数据,是否确认?", "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    console.log("确定");
                    callback ? callback(id) : ElMessage("未找到回调函数")
                })
                .catch((err: any) => {
                    console.log(err);
                    ElMessage({
                        type: "info",
                        message: "已取消删除",
                    });
                });
        },

            // 日期时间格式化
            formatTime(date: any) {
                if (!this.isDate(date)) {
                    date = new Date(date)
                }
                try {
                    const year = date.getFullYear();
                    const month = (date.getMonth() + 1).toString().padStart(2, '0');
                    const day = date.getDate().toString().padStart(2, '0');
                    const hours = date.getHours().toString().padStart(2, '0');
                    const minutes = date.getMinutes().toString().padStart(2, '0');
                    const seconds = date.getSeconds().toString().padStart(2, '0');
                    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
                } catch {
                    return '-'
                }
            },

            // 日期格式化
            formatDate(date: any) {
                if (!this.isDate(date)) {
                    date = new Date(date)
                }
                try {
                    const year = date.getFullYear();
                    const month = (date.getMonth() + 1).toString().padStart(2, '0');
                    const day = date.getDate().toString().padStart(2, '0');
                    return `${year}-${month}-${day}`;
                } catch {
                    return '-'
                }
            },
             // 判断是否为日期对象
             isDate(value: any) {
                // 通过 toString() 方法将 value 值转换为字符串类型
                const stringValue = Object.prototype.toString.call(value);
                // 判断是否为日期类型
                return stringValue === '[object Date]' && !isNaN(value.getTime());
            }
    }
})
export default Store